package com.dabai.kcbalarmclock;

import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.AlarmClock;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.bottomsheet.BottomSheetDialog;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Random;

public class OneKeyActivity extends AppCompatActivity {

    private BottomSheetDialog bsd;
    private String note;
    private String times;
    private String weeks;
    private String tip;
    private String TAG = "dabaizzz";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        bsd = new BottomSheetDialog(this);
        bsd.setContentView(R.layout.activity_one_key);
        bsd.show();

        bsd.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                finish();
            }
        });

        TextView te1 = bsd.findViewById(R.id.textView);

        try {
            Intent intent = getIntent();

            Uri uri = Uri.parse(intent.getDataString());
            note = uri.getQueryParameter("note");
            times = uri.getQueryParameter("times");
            weeks = uri.getQueryParameter("weeks");
            tip = uri.getQueryParameter("tip");

            te1.setText("" + note);

        } catch (Exception e) {
            bsd.dismiss();
            finish();
            Toast.makeText(this, "一键部署有点小问题!", Toast.LENGTH_SHORT).show();
        }


    }


    /**
     * 设置闹钟 自定义星期
     *
     * @param weekDays
     * @param message
     * @param hour
     * @param minutes
     * @param resId
     */
    private void createAlarm(ArrayList<Integer> weekDays, String message, int hour, int minutes, int resId) {
        Log.d(TAG, "createAlarm: " + resId);
        String packageName = getApplication().getPackageName();
        Uri ringtoneUri = Uri.parse("android.resource://" + packageName + "/" + resId);
        //action为AlarmClock.ACTION_SET_ALARM
        Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM)
                //闹钟的小时
                .putExtra(AlarmClock.EXTRA_HOUR, hour)
                //闹钟的分钟
                .putExtra(AlarmClock.EXTRA_MINUTES, minutes)
                //响铃时提示的信息
                .putExtra(AlarmClock.EXTRA_MESSAGE, message)
                //用于指定该闹铃触发时是否振动
                .putExtra(AlarmClock.EXTRA_VIBRATE, true)
                //一个 content: URI，用于指定闹铃使用的铃声，也可指定 VALUE_RINGTONE_SILENT 以不使用铃声。
                //如需使用默认铃声，则无需指定此 extra。
                .putExtra(AlarmClock.EXTRA_RINGTONE, ringtoneUri)
                //一个 ArrayList，其中包括应重复触发该闹铃的每个周日。
                // 每一天都必须使用 Calendar 类中的某个整型值（如 MONDAY）进行声明。
                //对于一次性闹铃，无需指定此 extra
                .putExtra(AlarmClock.EXTRA_DAYS, weekDays)
                //如果为true，则调用startActivity()不会进入手机的闹钟设置界面
                .putExtra(AlarmClock.EXTRA_SKIP_UI, false);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }

    public void onekey(View view) {

        Toast toast = Toast.makeText(getApplicationContext(), "请稍等几秒钟，不要关闭程序。", Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER,0,0);
        toast.show();

        new Thread(new Runnable() {
            @Override
            public void run() {

                String timesstr[] = times.split(",");
                String weeksstr[] = weeks.split(";");


                for (int i = 0; i < timesstr.length; i++) {
                    String starttime = timesstr[i];
                    //Log.d(TAG, "设置成功: "+starttime);
                    ArrayList<Integer> arrayList = getWeeksArray(weeksstr[i]);
                    if (arrayList == null) {
                        continue;
                    }

                    createAlarm(arrayList, tip, Integer.parseInt(starttime.split(":")[0]), Integer.parseInt(starttime.split(":")[1]), new Random().nextInt(1000));
                    try {
                        Thread.sleep(1300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }


            }
        }).start();
    }

    private ArrayList<Integer> getWeeksArray(String weeksstr) {
        if (weeksstr.equals("")) {
            return null;
        }
        ArrayList<Integer> weeksarr = new ArrayList<>();
        String weees[] = weeksstr.split(",");
        for (String weekstr : weees) {
            weeksarr.add(Integer.parseInt(weekstr) + 1);
        }
        return weeksarr;
    }


}
